package com.zhike.service.file;

import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import javax.annotation.Resource;

import org.liuxp.minioplus.api.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhike.common.component.PrepareParams;
import com.zhike.common.conf.IMBaseConf;
import com.zhike.common.constant.Constants;
import com.zhike.common.constant.SysConfigConstants;
import com.zhike.common.enums.YesNoEnum;
import com.zhike.common.utils.AliSecurityCheckUtilsV1;
import com.zhike.service.system.ISystemConfigService;

import lombok.extern.slf4j.Slf4j;

/**
 * 短视频消息中的首帧预览图文件上传处理实现类。
 * <p>
 * 首帧预览图用于用户收到短视频消息时，能在不解析视频的情况下高效地显示视频预览效果，提升体验。
 * 
 * @author Jack Jiang
 * @sin 4.4
 */
@Slf4j
@Component
public class ShortVideoThumbUploader extends UploaderRoot {

  @Resource
  private IMBaseConf baseConf;

  @Autowired
  private AliSecurityCheckUtilsV1 apAliSecurityCheckUtilsV1;

  @Autowired
  private ISystemConfigService systemConfigService;

  @Autowired
  private StorageService storageService;

  @Override
  public String getFileSaveDir() {
    return getShortVideoThumbSaveDir();
  }

  public String getShortVideoThumbSaveDir() {
    return baseConf.getDIR_USER_UPLOAD();
  }

  @Override
  public void onUploadSucess(HashMap<String, Object> parametersFromClient) throws Exception {
    String userUid = (String) parametersFromClient.get("user_uid");
    // 因为服务端是支持多文件上传的API，所以客户端单独把文件名带过来了，方便使用！
    String fileName = (String) parametersFromClient.get("file_name");

    String url = (String) parametersFromClient.get("url");

    log.debug("[" + getDebugTAG() + "] 用户" + userUid + "的短视频首帧预览图文件" + fileName + "数据上传成功！【OK】");

    Integer conf = systemConfigService.getValueByKeyAsInt(SysConfigConstants.CONFIG_TYPE_SECURITY,
        SysConfigConstants.CONFIG_MESSAGE);
    if (conf != null && conf.equals(YesNoEnum.NO.getValue())) {
      return;
    }

    if (StringUtils.isNotBlank(userUid) && StringUtils.isNotBlank(fileName) && StringUtils.isNotBlank(url)) {
      Boolean res = apAliSecurityCheckUtilsV1.checkVideo(new PrepareParams() {

        @Override
        public String getApiServerUrl() {
          return systemConfigService.getValueByKey(SysConfigConstants.CONFIG_TYPE_IM,
              SysConfigConstants.CONFIG_FILE_DOMAIN);
        }

        @Override
        public String getVideoUrl() {
          return url;
        }

      });

      if (!res) {
        storageService.remove(fileName);
        log.info("uid: {} 音频文件: {} 不合规 已删除", userUid, fileName);
        return;
      }
    }
  }

  @Override
  public void onUploadFailure(Exception ex) {
    log.error("[" + getDebugTAG() + "] 处理短视频首帧预览图上传时出错了，" + ex.getMessage(), ex);
  }

  @Override
  public String getDebugTAG() {
    return "HTTP短视频首帧";
  }

  @Override
  public String getPathTag() {
    return Constants.UPLOAD_CHAT + "/video";
  }

  @Override
  public Set<String> getDetectedTypes() {
    Set<String> allowExtensions = new HashSet<>();
    allowExtensions.add("video/quicktime");
    return allowExtensions;
  }
}
